package net.adetech.port.service.impl;

import net.adetech.port.entity.PCGTicket;
import net.adetech.port.service.ISyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class SyncServiceImpl implements ISyncService{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<PCGTicket> getPcgTicketList() {

        final String sql = "SELECT k.*,h.hb_time KCSJ,(SELECT name from SP_SHIP s where k.SHIPID=s.CODE) HBMC FROM (SELECT * from SP_KPJL where SP_TIME > dateadd(HOUR,-1,getdate())) k  LEFT JOIN (SELECT * from SP_HBJH where CONVERT(VARCHAR(32),hb_rq,111) = ? ) h on k.hbbh = h.hb_bh order by k.id desc";
        String date = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        List<PCGTicket> pcgTickets = jdbcTemplate.query(sql,new Object[]{date},new PcgTicketRowMapper());
        return pcgTickets;
    }



}

@Component
class PcgTicketRowMapper implements RowMapper<PCGTicket>{

    private String sfg = "长岛";

    private static Map<String,String> zdMap = new HashMap<String,String>();
    static {
        zdMap.put("01","蓬莱");
        zdMap.put("02","砣矶");
        zdMap.put("03","大钦");
        zdMap.put("04","小钦");
        zdMap.put("05","南隍");
        zdMap.put("06","北隍");
        zdMap.put("07","大黑山");
        zdMap.put("08","小黑山");
        zdMap.put("09","庙岛");
    }

    @Override
    public PCGTicket mapRow(ResultSet rs, int rownum) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        Set<String> fieldSet = new HashSet<String>();
        for (int i = 1; i <= columnCount; i++) {
            fieldSet.add(rsmd.getColumnName(i));
        }
        PCGTicket ticket = new PCGTicket();
        //系统主键
        if (fieldSet.contains("ID") && null != rs.getObject("ID")) {
            ticket.setId(rs.getString("ID"));
        }
        //


//    <旅客姓名>朱瑜</旅客姓名>
        if (fieldSet.contains("SFZXM") && null != rs.getObject("SFZXM")) {
            String xm = rs.getString("SFZXM");
            if (xm!=null)
            {
                xm = xm.trim();
            }
            ticket.setXm(xm);
        }
//    <证件号码>320520195003010125</证件号码>
        if (fieldSet.contains("NOTE1") && null != rs.getObject("NOTE1")) {
            ticket.setSfzh(rs.getString("NOTE1"));
        }
//    <证件类型>身份证</证件类型>NOTE1
        if (ticket.getSfzh()!=null&&ticket.getSfzh().length()==18)
        {
            ticket.setZjlx("身份证");
        }
//    <离港时间>2017/09/22 12:06:16</离港时间>
        if (fieldSet.contains("KCSJ") && null != rs.getObject("KCSJ")) {
            Timestamp kcsj = getFullDateFromTimeString(rs.getString("KCSJ"));
            ticket.setKcsj(kcsj);
        }
//    <班次>和航兴龙</班次>
        if (fieldSet.contains("HBMC") && null != rs.getObject("HBMC")) {
            ticket.setBc(rs.getString("HBMC"));
        }
//    <舱位>34</舱位>
        if (fieldSet.contains("XH") && null != rs.getObject("XH")) {
            ticket.setCw(rs.getString("XH"));
        }
//    <始发港>长岛</始发港>
        ticket.setSfg(sfg);
//    <到达港>蓬莱</到达港>
        if (fieldSet.contains("ZDID") && null != rs.getObject("ZDID")) {
            String zdid = rs.getString("ZDID").trim();
            ticket.setDdg(zdMap.get(zdid));
        }

//    <售票方式/>

//    <购票时间>2017-9-22 11:55:44</购票时间>
        if (fieldSet.contains("SP_TIME") && null != rs.getObject("SP_TIME")) {
            ticket.setSpsj(rs.getTimestamp("SP_TIME"));
        }
//    <货物类型/>
//    <货物重量/>

//    <入库时间>2017-9-22 16:18:51</入库时间>
        ticket.setRksj(new Timestamp(new Date().getTime()));
//    <更新时间>2017-9-22 16:18:51</更新时间>
        ticket.setGxsj(new Timestamp(new Date().getTime()));

        return ticket;
    }


    private static Timestamp getFullDateFromTimeString(String timeString)
    {
        Timestamp timestamp = null;
        try {
            timeString = timeString.substring(11,19);
            String dateString = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            timestamp = new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateString+" "+timeString).getTime());
        }
        catch (Exception e)
        {

        }
        return timestamp;
    }
}
